<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>表单验证</title>
        <script>
            function fun(){
                var name = document.forms["myform"]["fname"].value;
            if(name==""||name==null){
                alert("请填写名字");
                return false;
            }
            }
        </script>
    </head>
    <body>
        <form name="myform" action="" onsubmit="return fun()" method="post">
            姓名：<input type="text" name="fname">
            <input type="submit" value="提交">
        </form>
    </body>
</html>